package per.zx;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author zhengxin
 * @date 2021/7/6 23:02
 * @description
 */
public class TestCyclicBarrier {

    static CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
    private final  static Logger logger = LoggerFactory.getLogger(TestCyclicBarrier.class);
    public static void main(String[] args) throws BrokenBarrierException, InterruptedException {

        new Thread(() -> {
            try {

                logger.info("will wait");
                cyclicBarrier.await();
                logger.info("end wait");
            } catch (InterruptedException | BrokenBarrierException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(() -> {
            try {
                logger.info("will await");
                cyclicBarrier.await();
                logger.info("end wait");
            } catch (InterruptedException | BrokenBarrierException e) {
                e.printStackTrace();
            }
        }).start();

        logger.info("main sleep");
        Thread.sleep(2000);

        logger.info("main will await");
        cyclicBarrier.await();
        logger.info("main end wait");
    }
}
